/*
 * filter.js
 *
 * This file contains all of the information and functions relating to the filter menu on
 * the window.
 * 
 * @author Ben Co, Ben Gadbois, Michael Liedtke, Julia Weimar-East
 */

/*
 * createFilter()
 * 
 * This function creates a string of all the selected filters (queries) on the filter menu
 * by calling all of the individual "Query" functions below. 
 * 
 * @return string The compiled filter information string
 */
function createFilter() {
    var string =  "";
    
    if ($("#radio-zip").is(":checked")){
	string = string + zipQuery() + " AND "
    }
    else
    {
	string = string + stateQuery() + " AND "
    }
    string = string + genderQuery() + " AND ";
    if(!$("#all").hasClass("ui-selected")){
	string += collegeMajorQuery() + " AND ";
    }
    string += gpaQuery() + " AND " 
	+ satMathQuery() + " AND "
	+ yearQuery() + " AND "
	+ satReadingQuery();
    return string;
}

/*
 * zipQuery()
 * 
 * This function creates a string representing the zipcode query information from the 
 * filter menu. This includes the zip code(s) selected by the user.
 * 
 * @return zipQueryString The zip code information
 */
function zipQuery() {
    var zipFieldString = $("#zipcode").val();
    var zipCodes = zipFieldString.split(/(?:,| )+/);
    var zipQueryString = "'Zip' IN('" + zipCodes[0];
    for(var i = 1; i < zipCodes.length; i++){
	    zipQueryString += "', '" + zipCodes[i];
    }
    zipQueryString += "')";
    return zipQueryString;
}

/*
 * stateQuery()
 * 
 * This function creates a string containing the state query information from the 
 * filter menu. This includes the state selected by the user. 
 * 
 * @return The state abbreviation corresponding to what is selected in filter menu
 */
function stateQuery() {
    return "State = '" + 
	cs441GoogleMapsViz.convertRegionToState($("#state").val()) + "'";
}

/*
 * genderQuery()
 * 
 * This function creates a string containing the gender query information from the 
 * filter menu. This includes the gender(s) selected by the user or the default of
 * both genders.
 * 
 * @return The gender information string
 */
function genderQuery() {
    if ($('#male').is(':checked') && $('#female').is(':checked')) {
	    return "Gender IN ('M','F')";
    } else if ($('#male').is(':checked')) {
	    return "Gender = 'M'"
    } else if ($('#female').is(':checked')) {
	    return "Gender = 'F'"
    } else {
	    return "Gender = 'DO_NOT_DISPLAY'";
    }
}

/*
 * gpaQuery()
 * 
 * This function creates a string containing all of the information for the GPA
 * query from the filter menu. This includes the range of values selected by the
 * user or the default values.
 * 
 * @return The gpa information string
 */
function gpaQuery() {
    return "'HS_GPA' <= " + 
	parseFloat($("#gpa-high").val()) + 
	" AND 'HS_GPA' >= " +
	parseFloat($("#gpa-low").val());
}

/*
 * satMathQuery()
 * 
 * This function creates a string containing the SAT Math score query information from 
 * the filter menu. This includes the range of values selected by the user or the 
 * default values.
 * 
 * @return the SAT Math score information string
 */
function satMathQuery() {
    return "'SAT_MAth' <= " + 
	parseFloat($("#sat-math-high").val()) + 
	" AND 'SAT_MAth' >= " +
	parseFloat($("#sat-math-low").val());
}

/* 
 * satReadingQuery()
 * 
 * This function creates a string containing all of the SAT Reading query information
 * from the filter menu. This includes the range of values selected by the user or the 
 * default values.
 * 
 * @return The SAT Reading score information string
 */
function satReadingQuery() {
    return "'SAT_Verbal' <= " + 
	parseFloat($("#sat-reading-high").val()) + 
	" AND 'SAT_Verbal' >= " +
	parseFloat($("#sat-reading-low").val());
}

/*
 * yearQuery()
 * 
 * This function creates a string containing the year query information selected by the 
 * user or both years 2011 and 2012 as the default.
 * 
 * @return The year information string
 */
function yearQuery() {
    if ($('#2011').is(':checked') && $('#2012').is(':checked')) {
        return "Entry_Year IN ('2011','2012')";
    } else if ($('#2011').is(':checked')) {
        return "Entry_Year = '2011'"
    } else if ($('#2012').is(':checked')) {
        return "Entry_Year = '2012'"
    } else {
        return "Entry_Year = 'DO_NOT_DISPLAY'";
    }
}

function collegeMajorQuery () {
    if($("#all").hasClass("ui-selected")){
	return '';
    }
    else {
	var query = "Planned_Major_Code IN (9999";
	$("li.ui-selected").each(function(){
	    query+= ', ' + this.value;
	});
	query += ')';	
	return query;
    }
}

/*
  code to build a query for a range of zips
  Removed because it would not function for ranges too large
    if (zipFieldString.match(/\d{5}-\d{5}/)) {
	var zipCodeLow = parseInt(zipFieldString.replace(/-\d{5}/,""));
	var zipCodeHigh = parseInt(zipFieldString.replace(/\d{5}-/,""));
	var zipQueryString = "'Zip' IN ('";
	for (var i = zipCodeLow; i < zipCodeHigh; i++ ) {
	    zipQueryString += (i + "', '" );
	}
	zipQueryString += zipCodeHigh + "')";
	return zipQueryString;
    }
*/
